package com.itboye.hutouben.volley;

import android.content.Intent;

import com.android.volley.ServerError;
import com.android.volley.VolleyError;
import com.itboye.hutouben.activity.mysetting.SettingActivity;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.logincontroller.LoginController;
import com.itboye.hutouben.logincontroller.UnLoginState;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.SPUtils;
import com.umeng.message.UTrack;

public class BaseErrorListener implements XErrorListener {
	
	private ICompleteListener listener;
	public BaseErrorListener(ICompleteListener listener){
		this.listener = listener;
	}
	
	@Override
	public void onErrorResponse(Exception exception, String code, String msg) {
//			Log.v("error", exception.getMessage());
		if(listener != null){
				
			if(code == "0"){
					code = "-199";
			}
			else if(code.equals("1111")){
				LoginController.setLoginState(new UnLoginState());
				deleteInfo();
				Intent intent2 = new Intent(SettingActivity.EXITCHANGE);// 通知MeFragment更新界面
				MyApplcation.ctx.sendBroadcast(intent2);
				ResultEntity result = new ResultEntity(code,msg, exception);
				listener.failure(result);
				return;
			}
			else if(msg == null || msg.equals("")){
				if(exception != null){
					msg = exception.getMessage();
				}else{
					msg = "发生未知错误!";
				}
			}
			else if(exception instanceof ServerError){
				ServerError error = (ServerError) exception;
				code = ""+ error.networkResponse.statusCode;
				msg = "服务器返回错误!";
			}
			else if(exception instanceof VolleyError){
				VolleyError jsonException=(VolleyError) exception;
				String error=jsonException.getLocalizedMessage();
				msg = "服务器开小差了...";
			}
			ResultEntity result = new ResultEntity(code,msg, exception);
			listener.failure(result);
		}				
	}
	private void deleteInfo() {
		LoginController.setLoginState(new UnLoginState());
		MyApplcation.getInstance().mPushAgent.removeAlias(
				SPUtils.get(MyApplcation.ctx, null, Const.ID, "") + "", "htb",
				new UTrack.ICallBack() {

					@Override
					public void onMessage(boolean arg0, String arg1) {
						// TODO Auto-generated method stub
						System.out.println("---退出登录>"+arg0+"---"+arg1);
					}
				});
		SPUtils.put(MyApplcation.ctx, null, Const.ID, "");
		SPUtils.put(MyApplcation.ctx, null, Const.USERNAME, "");
		SPUtils.put(MyApplcation.ctx, null, Const.PASSWORD, "");
		SPUtils.put(MyApplcation.ctx, null, Const.MOBILE, "");
		SPUtils.put(MyApplcation.ctx, null, Const.IS_LOGINED, false);
		SPUtils.put(MyApplcation.ctx, null, Const.HEAD, "");
		SPUtils.put(MyApplcation.ctx, null, Const.NICK, "");
		SPUtils.put(MyApplcation.ctx, null, Const.PaySecret, "");
		SPUtils.put(MyApplcation.ctx, null, Const.S_ID, "");

		LoginController.setLoginState(new UnLoginState());

	}
}

